package service;

import dao.CommonDAO;
import dao.CourseDao;
import entity.Operator;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class OperatorService{
    private CommonDAO cd = new CommonDAO();

    CourseDao courseDao = new CourseDao();

    public Operator getOperatorById(String operatorId){
        return courseDao.getOperatorById(operatorId);
    }

    public int getNumberOfOperator(){ return courseDao.getNumberOfOperator();}

    public void passwordChange(String operatorId, String newPassword){
        courseDao.passwordChange(operatorId, newPassword);
    }

    public void updateCharacter(String operatorId, String character){
        courseDao.updateCharacter(operatorId, character);
    }

    public List<Operator> operators(){
        List<Operator> list=new ArrayList<>();

        String sql="select * from operator";
        List<Map<String,Object>> mapList = cd.executeQuery(sql, new Object[0]);

        for(int i=0;i<mapList.size();i++){
            Map<String, Object> tempMap = mapList.get(i);
            list.add(new Operator(
                    Integer.parseInt(tempMap.get("operatorID").toString()),
                    tempMap.get("oname").toString(),
                    tempMap.get("account").toString(),
                    tempMap.get("password").toString(),
                    Integer.parseInt(tempMap.get("characters").toString()),
                    tempMap.get("email").toString(),
                    tempMap.get("sex").toString(),
                    tempMap.get("photo").toString()
            ));
        }
        return list;
    }

    public List<Object> login(String account, String password){
        String sql = "select * from operator where account=? and password=?";
        List<Map<String,Object>> lm = cd.executeQuery(sql, new Object[]{account,password});

        List<Object> rl = new ArrayList<>();

        //登录成功
        if(lm.size()>0){
            Map<String,Object> operatorMap = lm.get(0);
            Operator operator = new Operator();

            operator.setAccount(operatorMap.get("account").toString());
            operator.setPassword(operatorMap.get("password").toString());
            operator.setName(operatorMap.get("oname").toString());
            operator.setOperatorID(Integer.parseInt(operatorMap.get("operatorID").toString()));
            operator.setCharacter(Integer.parseInt(operatorMap.get("characters").toString()));
            operator.setEmail(operatorMap.get("email").toString());
            operator.setSex(operatorMap.get("sex").toString());
            operator.setPhoto(operatorMap.get("photo").toString());

            rl.add(true);
            rl.add(true);
            rl.add(operator);
        }
        //登录不成功 可有有两种原因，1.帐号不存在 2.密码错误
        else{
            sql=  "select * from operator where account=?";
            lm = cd.executeQuery(sql, new Object[]{account});
            if(lm.size()==0){//用户不不存在错误
                System.out.println("this");
                rl.add(false);
                rl.add(true);
            }else{//密码错误
                rl.add(true);
                rl.add(false);
            }
            rl.add(null);
        }
        return rl;
    }
    public int delete(Integer operatorID){
        String sql="delete from operator where operatorID=?";
        int count=cd.executeUpdate(sql,new Object[]{operatorID});
        return count;
    }
    public int update(Integer operatorID,String oname,String email,String sex){
        String sql="update operator set oname=?,email=?,sex=? where operatorID=?";
        int count=cd.executeUpdate(sql,new Object[]{oname,email,sex,operatorID});
        return count;
    }
    public int insert(Integer operatorID, String oname, String account, String password, Integer character, String email, String sex, String photo){
        String sql="insert(operatorID,oname,account,password,character,email,sex,photo) into operator values(?,?,?,?,?,?,?,?)";
        int count=cd.executeUpdate(sql,new Object[]{operatorID,oname,account,password,character,email,sex,photo});
        return count;
    }
}
